﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Linq;
using PSE.Deployment.SetupBase.Configuration;
using PSE.Deployment.SetupBase.Configuration.Enums;
using PSE.Deployment.SetupBase.Parameter;
using PSE.Deployment.Engine.Interfaces;
using PSE.Deployment.ConfigurationManager.Engine.Operations;
using PSE.Deployment.Util;
using PSE.Deployment.SetupBase.Parameter.Enums;

namespace PSE.Deployment.SetupBase.Steps
{
    public class UpdateConfig : SetupStep<Configuration.Actions.UpdateConfig>
    {
        public UpdateConfig(IStepGroup parent, Group group, Configuration.Actions.UpdateConfig action, Parameter.Enums.SetupMode setupMode)
            : base(parent, group, action, setupMode)
        {
            this.ResetProgress();
        }

        public override void Execute()
        {
            this.ResetProgress();

            if (this.SetupMode == Parameter.Enums.SetupMode.Install)
            {
                UpdateConfigs();
            }
            else if (this.SetupMode == Parameter.Enums.SetupMode.Uninstall)
            {
                UninstallArtefacts();
            }
        }

        public override void Undo()
        {
            if (this.SetupMode == Parameter.Enums.SetupMode.Install)
            {
                UninstallArtefacts();
            }
        }

        private void ResetProgress()
        {
            int count = 0;

            foreach (XmlFile xmlFile in Action.XmlFiles)
            {
                string path;

                if (Directory.Exists(SetupMainFactory.GetInstance().Parameter.InstalationSource))
                    path = Path.Combine(SetupMainFactory.GetInstance().Parameter.InstalationSource, xmlFile.Folder.Source);
                else
                    path = Path.Combine(SetupMainFactory.GetInstance().Parameter.InstallationPath, xmlFile.Folder.Destination);

                if (xmlFile.Folder.Files.Count == 0 && Directory.Exists(path))
                {
                    count += PSE.Deployment.Util.File.CountFiles(path, "*.config");
                }
                else
                {
                    count += xmlFile.Folder.Files.Count;
                }
            }

            this.ResetProgress(count);
        }

        private void UpdateConfigs()
        {
            foreach (XmlFile xmlFile in Action.XmlFiles)
            {
                string path = Path.Combine(SetupMainFactory.GetInstance().Parameter.InstallationPath, xmlFile.Folder.Destination);

                if (xmlFile.Folder.Files.Count == 0 && Directory.Exists(path))
                {
                    Update(path, xmlFile, Directory.GetFiles(path, "*.config", SearchOption.AllDirectories));
                }
                else
                {
                    Update(path, xmlFile, xmlFile.Folder.Files);
                }
            }
        }

        private void Update(string path, XmlFile xmlFile, IEnumerable<string> files)
        {
            foreach (string file in files)
            {
                if (this.IsStopped())
                    break;

                string config;

                if (Path.IsPathRooted(file))
                {
                    config = file;
                }
                else
                {
                    config = Path.Combine(path, file);
                }

                try
                {
                    this.IncrementProgress(config);

                    XmlOperations operation = new XmlOperations(SetupMainFactory.GetInstance().Parameter);

                    //if (config.ToLower().Contains("settings"))
                    //{
                    //    if (SetupMainFactory.GetInstance().Parameter.Prerequisites.IIS7 == PrerequisitesStatus.Ok)
                    //    {
                    //        operation.SaveFile(xmlFile, config, this.Group.System.Id, true);
                    //    }
                    //}
                    //else
                    //{
                        operation.SaveFile(xmlFile, config, this.Group.System.Id, true);
                    //}

                    this.AddUsedBy(config);
                }
                catch
                {
                    this.DecrementProgress(config);
                    throw;
                }
            }
        }

        protected override void UninstallArtefact(string artefactName)
        {
            System.Threading.Thread.Sleep(20);
        }
    }
}
